package com.acmen.easyapi.repository;

import com.acmen.easyapi.dbcp.JdbcService;
import com.acmen.easyapi.model.TestCase;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

;

/**
 * @author acmen
 */
@Component
public class TestCaseRepository {
    private final static String QUERY_SQL = "select * from t_test_case where 1=1";
    private final static String QUERY_SINGLE_ROW_SQL = "select * from t_test_case where id=? ";
    private final static String INSERT_SQL = "insert into t_test_case(id,app_name,name,env) values(?,?,?,?)";
    private final static String UPDATE_SQL = "update t_test_case set app_name=?,name=?,env=? where id=?";
    private final static String DELETE_SQL = "delete from t_test_case where id=?";
    @Resource
    private JdbcService jdbcService;

    /**
     *
     * @param query
     * @param args
     * @return
     */
    public List<TestCase> selectList(String query, Object[] args) {
        List<TestCase> testCases = jdbcService.selectList(QUERY_SQL+ " and " +query, args,TestCase.class);
        return testCases;
    }

    public TestCase selectOne(String id){
        TestCase testCase =  jdbcService.selectOne(QUERY_SINGLE_ROW_SQL, new Object[]{id},TestCase.class);
        return testCase;
    }

    /**
     * 保存测试用例
     *
     * @param testCase
     * @return
     */
    public boolean create(TestCase testCase) {
        Object[] args = {testCase.getId(), testCase.getAppName(), testCase.getName(), testCase.getEnv()};
        return jdbcService.update(INSERT_SQL, args) > 0;
    }

    public boolean update(TestCase testCase) {
        Object[] args = {testCase.getAppName(), testCase.getName(), testCase.getEnv(),testCase.getId()};
        return jdbcService.update(UPDATE_SQL, args) > 0;
    }

    /**
     * 删除测试用例记录
     *
     * @param id
     * @return
     */
    public boolean delete(String id) {
        return jdbcService.update(DELETE_SQL, new Object[]{id}) > 0;
    }

}
